package com.diana.serviceImpl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.diana.common.Utils;
import com.diana.entity.QueueMain;
import com.diana.mapper.QueueMainMapper;
import com.diana.service.QueueMainService;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 序列字典表 服务实现类
 * </p>
 *
 * @author anzhi
 * @since 2019-05-04
 */
@Service
public class QueueMainServiceImpl extends ServiceImpl<QueueMainMapper, QueueMain> implements QueueMainService {

    Lock lock = new ReentrantLock();

    /**
     * 创建序列
     * @param map
     * @return
     */
    @Override
    public String createQueue(Map<String, Object> map) {
        String queueName = Utils.toString(map.get("queueName"));
        String queueAlias = Utils.toString(map.get("queueAlias"));
        String queuePrefix = Utils.toString(map.get("queuePrefix"));
        Integer queueValue = Utils.toInteger(map.get("queueValue"));
        if(!Utils.checkEngStr(queueName,4,20)){
            return Utils.message("0001","queueName命名不规范",null);
        }
        if(queueValue==null||queueValue<0){
            return Utils.message("0001","queueValue为大于0的整数",null);
        }
        QueueMain queueMain = selectOne(Condition.create().eq("queueName", queueName));
        if(queueMain!=null){
            return Utils.message("0001","queueName已存在",null);
        }
        queueMain = new QueueMain();
        queueMain.setQueueAlias(queueAlias);
        queueMain.setQueueName(queueName);
        queueMain.setQueuePrefix(queuePrefix);
        queueMain.setQueueValue(queueValue);
        if(insert(queueMain)){
            return Utils.message("0000","操作成功",null);
        }
        return Utils.message("0001","操作失败",null);
    }

    /**
     * 获取Id
     * @param queueName
     * @return
     */
    @Override
    public String getQueueValue(String queueName) {
        String value = null;
        lock.lock();
        do {
            try {
                QueueMain queueMain = selectOne(Condition.create().eq("queueName", queueName));
                if(queueMain!=null){
                    String queuePrefix = queueMain.getQueuePrefix();
                    Integer queueValue = queueMain.getQueueValue();
                    Integer queueId = queueMain.getQueueId();
                    queueMain = new QueueMain();
                    queueMain.setQueueId(queueId);
                    queueMain.setQueueValue(queueValue+1);
                    if(updateById(queueMain)){
                        value = queuePrefix+queueMain.getQueueValue();
                    }
                }else{
                    break;
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }while (value==null);
        return value;
    }

    @Override
    public String getQueueValueNoPre(String queueName) {
        String value = null;
        lock.lock();
        do {
            try {
                QueueMain queueMain = selectOne(Condition.create().eq("queueName", queueName));
                if(queueMain!=null){
                    String queuePrefix = queueMain.getQueuePrefix();
                    Integer queueValue = queueMain.getQueueValue();
                    Integer queueId = queueMain.getQueueId();
                    queueMain = new QueueMain();
                    queueMain.setQueueId(queueId);
                    queueMain.setQueueValue(queueValue+1);
                    if(updateById(queueMain)){
                        value = Utils.toString(queueMain.getQueueValue());
                    }
                }else{
                    break;
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }while (value==null);
        return value;
    }
}
